/**********************************************************************************
 * Copyright (c) 2012 - 2012,  parallel, All right reserved
 * Title: com.adc.parallel.pool  ThreadPool.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-11-22 下午2:36:59 
 *********************************************************************************/


package com.adc.parallel.pool;

import com.adc.litepool.ObjectPoolHandler;
import com.adc.litepool.impl.StackObjectPoolHandlerFactory;
import com.adc.parallel.constants.SysConstants;


/**
 * 
 * Description: define global executor thread pool
 * @author shen  shenqiao2012@163.com
 */
public abstract class ThreadPool {
	private static ThreadPoolFactory factory;
	private static ObjectPoolHandler pool;
	
	/**
	 * 
	 * Description: initialization
	 * author:  shen
	 * @param executorThread 
	 * @return void
	 */
	public static <T> void init(T executorThread) {
		pool=StackObjectPoolHandlerFactory.getInstance().createPool(
				getThreadPoolFactory(), SysConstants.getMaxPoolSize(), executorThread.getClass());

	}
	
	
	/**
	 * 
	 * Description: get pool handler 
	 * author:  shen
	 * @param <T>
	 * @return ObjectPoolHandler
	 */
	public synchronized static  <T> ObjectPoolHandler getPoolHandler(T executorThread) {
		if (pool==null) {
			init(executorThread);
		}
		return pool;
	}
	
	/**
	 * 
	 * Description: create pool factory 
	 * author:  shen
	 * @return ThreadPoolFactory
	 */
	public synchronized static ThreadPoolFactory getThreadPoolFactory(){
		if (factory==null) {
			factory=ThreadPoolFactory.getInstance();
		}
		return factory;
	}


	/**
	 * @return the pool
	 */
	public static ObjectPoolHandler getCurrentPool() {
		return pool;
	}
	
}
